package com.cloudspace.jindun.activity.interphone;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.android.volley.error.VolleyError;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.adapter.interphone.ChannelAddMemberAdapter;
import com.cloudspace.jindun.model.ChannelInfoResult;
import com.cloudspace.jindun.model.ContactPersonListResult;
import com.cloudspace.jindun.model.Person;
import com.cloudspace.jindun.module.ActionItem;
import com.cloudspace.jindun.network.api.API;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ToastMaker;

import org.apache.http.HttpStatus;

import java.util.List;

/**
 * Created by zengxianhua on 17/2/11.
 */

public class ChannelAddMemberActivity extends CustomTitleBaseActivity{
    private List<Person> mContactPersons;
    private ChannelAddMemberAdapter mChannelAddMemberAdapter;
    private ListView mChannelAddMemberListView;

    private String mIsAlreadyMemberList;

    private String mChannelId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_channel_add_member, true);

        getTitleBar().setSimpleMode(getResources().getString(R.string.channel_add_contact),
                new ActionItem(getResources().getString(R.string.channel_add_complete), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        addChannelMember();
                    }
                })
                );

        if (getIntent() != null){
            mChannelId = getIntent().getStringExtra("channelid");
            mIsAlreadyMemberList = getIntent().getStringExtra("memberstr");
        }

        initView();
    }

    public static void showActivity(Context context, String channelId, String memberStr){
        Intent intent = new Intent(context, ChannelAddMemberActivity.class);
        if (!(context instanceof Activity)){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        intent.putExtra("channelid", channelId);
        intent.putExtra("memberstr", memberStr);
        context.startActivity(intent);
    }

    private void initView() {
        mChannelAddMemberListView = (ListView) findViewById(R.id.channel_add_member_listview);
        mChannelAddMemberAdapter = new ChannelAddMemberAdapter(this);
        mChannelAddMemberListView.setAdapter(mChannelAddMemberAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();

        API.getInstance().getOrgApi().getContactPersons(TAG, UserSessionManager.getInstance().getUserId(),
                new ApiCallback<ContactPersonListResult>() {
                    @Override
                    public void handleResult(ContactPersonListResult result, VolleyError error) {
                        if (result != null && result.status == HttpStatus.SC_OK){
                            mContactPersons = result.data;
                            mChannelAddMemberAdapter.setDatas(result.data, mIsAlreadyMemberList);
                        }else if (error != null){
                            ToastMaker.showToastShort(error.getMessage());
                        }
                    }
                });

    }

    private void addChannelMember() {
        List<Integer> selectStatus = mChannelAddMemberAdapter.getChecked();

        String selectedIds = "";
        for (int i = 0; i < mContactPersons.size(); ++i){
            if (selectStatus.get(i) == 1){
                selectedIds = selectedIds + mContactPersons.get(i).getUserId() + ",";
            }
        }

        if (TextUtils.isEmpty(selectedIds)){
            ToastMaker.showToastShort(getResources().getString(R.string.channel_add_member_empty));
            return;
        }

        selectedIds = selectedIds.substring(0, selectedIds.length() - 1);
        API.getInstance().getChannelApi().addChannelMember(TAG, mChannelId, selectedIds, new ApiCallback<ChannelInfoResult>() {
            @Override
            public void handleResult(ChannelInfoResult result, VolleyError error) {
                if (result != null && result.status == HttpStatus.SC_OK && error == null){
                    finish();
                }else{
                    ToastMaker.showToastShort(getResources().getString(R.string.channel_add_member_error) + error.getMessage());
                }
            }
        });
    }
}
